﻿@model AddProductToOrderSearchModel

@{
    //page title
    ViewBag.PageTitle = string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId);
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Orders");
}

@{
    const string hideSearchBlockAttributeName = "AddProductToOrderPage.HideSearchBlock";
    var hideSearchBlock = genericAttributeService.GetAttribute<bool>(workContext.CurrentCustomer, hideSearchBlockAttributeName);
}

<div class="content-header clearfix">
    <h1 class="pull-left">
        @string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId)
        <small>
            <i class="fa fa-arrow-circle-left"></i>
            <a asp-action="Edit" asp-route-id="@Model.OrderId">@T("Admin.Orders.Products.AddNew.BackToOrder")</a>
        </small>
    </h1>
    <div class="pull-right">
        &nbsp;
    </div>
</div>

<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row search-row @(!hideSearchBlock ? "opened" : "")"  data-hideAttribute="@hideSearchBlockAttributeName">
                        <div class="search-text">@T("Admin.Common.Search")</div>
                        <div class="icon-search"><i class="fa fa-search" aria-hidden="true"></i></div>
                        <div class="icon-collapse"><i class="fa fa-angle-down" aria-hidden="true"></i></div>
                    </div>

                    <div class="search-body @(hideSearchBlock ? "closed" : "")">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchProductName" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-editor asp-for="SearchProductName" />
                                    </div>
                                </div>
                                <div class="form-group" @(Model.AvailableCategories.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchCategoryId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                                    </div>
                                </div>
                                <div class="form-group" @(Model.AvailableManufacturers.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchManufacturerId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <nop-label asp-for="SearchProductTypeId" />
                                    </div>
                                    <div class="col-md-8">
                                        <nop-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-md-8 col-md-offset-4">
                                        <button type="button" id="search-products" class="btn btn-primary btn-search">
                                            <i class="fa fa-search"></i>
                                            @T("Admin.Common.Search")
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-body">
                    @await Html.PartialAsync("Table", new DataTablesModel
                    {
                        Name = "products-grid",
                        UrlRead = new DataUrl("AddProductToOrder", "Order", new RouteValueDictionary { [nameof(Model.OrderId)] = Model.OrderId }),
                        SearchButtonId = "search-products",
                        Length = Model.PageSize,
                        LengthMenu = Model.AvailablePageSizes,
                        Filters = new List<FilterParameter>
                        {
                            new FilterParameter(nameof(Model.SearchProductName)),
                            new FilterParameter(nameof(Model.SearchCategoryId)),
                            new FilterParameter(nameof(Model.SearchManufacturerId)),
                            new FilterParameter(nameof(Model.SearchProductTypeId))
                        },
                        ColumnCollection = new List<ColumnProperty>
                        {
                            new ColumnProperty(nameof(ProductModel.Id))
                            {
                                Title = T("Admin.Common.Select").Text,
                                Render = new RenderCustom("renderColumnSelectProduct"),
                                ClassName =  NopColumnClassDefaults.Button,
                                Width = "50",
                            },
                            new ColumnProperty(nameof(ProductModel.Name))
                            {
                                Title = T("Admin.Orders.Products.AddNew.Name").Text,
                                Width = "400"
                            },
                            new ColumnProperty(nameof(ProductModel.Sku))
                            {
                                Title = T("Admin.Orders.Products.AddNew.SKU").Text,
                                Width = "100"
                            }
                        }
                    })
                    
                    <script>
                        function renderColumnSelectProduct(data, type, row, meta) {
                            return '<button type="button" onclick="addProductToOrder(' + row.Id + ' )" class="btn btn-default">@T("Admin.Common.Select")</button>';
                        }
                        function addProductToOrder(productId) {
                            //load "product details page" block
                            var productDetailsActionUrl = '@Html.Raw(Url.Action("AddProductToOrderDetails", "Order", new {orderId = Model.OrderId, productId = "productidplaceholder"}))';
                            productDetailsActionUrl = productDetailsActionUrl.replace("productidplaceholder", productId);
                            setLocation(productDetailsActionUrl);
                        }
                    </script>
                </div>
            </div>
            <div id="productdetails">
            </div>
            <input asp-for="OrderId" type="hidden" />
        </div>
    </div>
</div>